// pages/index/groupPurchase/groupPurchase.js
var WxParse = require('../../../wxParse/wxParse.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      // 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    scrolls1: [
     
    ],
    scrolls2: [
      // { 'roomType': '14:30~19:45' },
      // { 'roomType': '14:30~19:45' },
      // { 'roomType': '14:30~19:45' },
      // { 'roomType': '14:30~19:45' },
    ],
    currentIndex: 0,
    currentInde: 0,
    // cons: [
    //   { 'img': 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg', 'count': '10438', 'priceN': '59', 'priceO': '108' },
    //   { 'img': 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg', 'count': '10438', 'priceN': '59', 'priceO': '108' },
    //   { 'img': 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg', 'count': '10438', 'priceN': '59', 'priceO': '108' },
    // ],
    rich:"<div style='height:100px;width:200px;'>asdfsadf</div>",
    // inde:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // richWord(){
  //   console.log(11)
  //   var that = this;
  //   console.log(that.data.rich)
  //   var rich = that.data.rich;
  //   WxParse.wxParse('rich','html',rich,that,5)
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.richWord()
    var _this = this;
    var today = new Date().getDay();
    var scrolls1 = [];
    var that = this;
    for (var i = 0; i < 7; i++) {
      scrolls1.push((today + i) % 7)
    }
    for (var i = 0; i < scrolls1.length; i++) {
      switch (scrolls1[i]) {
        case 0:
          scrolls1[i] = '周日';
          break;
        case 1:
          scrolls1[i] = '周一';
          break;
        case 2:
          scrolls1[i] = '周二';
          break;
        case 3:
          scrolls1[i] = '周三';
          break;
        case 4:
          scrolls1[i] = '周四';
          break;
        case 5:
          scrolls1[i] = '周五';
          break;
        case 6:
          scrolls1[i] = '周六';
          break;
      }
    }
    // console.table(scrolls1);
    that.setData({
      scrolls1: scrolls1
    })
    wx.request({
      url: app.globalData.baseUrl + '/api/commodity/index',
      data: {
        session_key: app.gs()
      },
      success: function (res) {
        console.log(res);
        // console.log(res.data.data.banner[0].start_time)
        var start = new Date().setTime(res.data.data.banner[0].start_time);
        console.log(start)
        _this.setData({
          banner: res.data.data.banner,
          shop: res.data.data.shop
        })
      }
    })
    // console.log(_this.data.cons.time[0].start_time)
    // console.log(_this.data.cons.time[0].end_time)
    wx.request({
      url: app.globalData.baseUrl + '/api/commodity/commodity',
      data: {
        session_key: app.gs(),
        week: _this.data.scrolls1[_this.data.currentIndex].substring(1, 2),
        // start_time: _this.data.cons.time[0].start_time,
        // end_time: _this.data.cons.time[0].end_time
      },
      success: function (res) {
        console.log('res---------', res)
        let cons = res.data.data;
        _this.setData({
          cons: cons
        })
        if (!cons) {
          console.log('return')
          return
        }
        let wxParseArr = []
        for (let i = 0; i < res.data.data.length; i++) {
          for (let j = 0; j < res.data.data[i].list.length; j++) {
            WxParse.wxParse('rich', 'html', res.data.data[i].list[j].content, that, 5)
            console.log('--------', WxParse.wxParse('rich', 'html', res.data.data[i].list[j].content, that, 5))
            wxParseArr.push(_this.data.rich)
          }
        }
        _this.setData({
          wxParseArr: wxParseArr,
          cons: cons
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  hitMouse1(e) {
    // console.log(e)
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    })
    let that = this;
    let _this = this;
    wx.request({
      url: app.globalData.baseUrl + '/api/commodity/commodity',
      data: {
        session_key: app.gs(),
        week: _this.data.scrolls1[_this.data.currentIndex].substring(1, 2),
      },
      success: function (res) {
        console.log(res)
        let cons = res.data.data;
        if(!cons){
          cons:[]
          _this.setData({
            cons: cons
          })
          console.log('return')
          return
        }
        console.log('wxRich')
        let wxParseArr = []
        for (let i = 0; i < cons.length; i++) {
          wxParseArr[i] = []
          for(let j=0;j<cons[i].list.length;j++){
            WxParse.wxParse('rich', 'html', cons[i].list[j].content, that, 5)

            // console.log('----------', WxParse.wxParse('rich', 'html', cons[i].list[j].content, that, 5))
            wxParseArr[i].push(_this.data.rich)

            // wxParseArr.push(_this.data.rich)
          }
        }
        _this.setData({
          wxParseArr: wxParseArr,
          cons: cons
        })
      }
    })
  },
  hitMouse2(e) {
    this.setData({
      currentInde: e.currentTarget.dataset.inde
    })
  },
  groupPurchaseDetail(e){
    console.log(e)
    var that = this;
    if (this.data.active == e.currentTarget.dataset.index ) {
      // 当当前点开状态，直接return
      return this.setData({
        active: 'noshow'
      })
    }
    console.log('2222')
    that.setData({
      active:e.currentTarget.dataset.index
    })
  },
  moreGroupPurchaseDetail(e){
    wx.navigateTo({
      url: 'moreGroupPurchaseDetail/moreGroupPurchaseDetail?com_id='+e.currentTarget.id,
    })
  },
  address(e){
    var that = this;
    wx.openLocation({
      latitude: parseInt(that.data.shop.latitude),
      longitude: parseInt(that.data.shop.longitude),
    })
  },
  phone(e){
    wx.makePhoneCall({
      phoneNumber: this.data.shop.photo
    })
  },
  orderNow(e){
    console.log('ididididi',e)
    wx.navigateTo({
      url: 'groupOrder/groupOrder?com_id=' + e.currentTarget.id+'&price='+e.currentTarget.dataset.price+'&mobile='+e.currentTarget.dataset.mobile+'&coupon=0'+'&groupName='+e.currentTarget.dataset.groupname,
    })
  },
  
})